/*
Copyright 2010 Sieric
http://sieric.ca

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vanilla;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;



@SuppressWarnings("serial")
public class NewProject extends JComponent implements ActionListener {
	private Viewer parentViewer;
	private Section sSource, sDestination, sIgnore;
	
	public NewProject(Viewer _v) {
		JLabel tmplbl;
		JSeparator tmpsep;
		JButton tmpbut;
		
		int y = 0;
		int width = 500;
		int height;
		
		parentViewer = _v;
		
		// Show the title
		JLabel title = new JLabel("New Project");
		title.setFont(title.getFont().deriveFont(Font.BOLD, 16));
		title.setBounds(1, y, width, 20);
		y += 30;
		add(title);
		
		// Show the Source separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Source location:");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show the source
		Section_Choose choose = new Section_Choose();
		sSource = choose;
		height = choose.getBigHeight();
		choose.setBounds(1, y, width, height);
		y += height+10;
		add(choose);
		
		// Show the Destination separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Destination location:");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show the destination
		Section_Choose local = new Section_Choose();
		sDestination = local;
		height = local.getBigHeight();
		local.setBounds(1, y, width, height);
		y += height+10;
		add(local);
		
		// Show the ignore separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Ignore files and folders:");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show the ignore
		Section_Ignore ignore = new Section_Ignore();
		height = ignore.getBigHeight();
		sIgnore = ignore;
		ignore.setBounds(1, y, width, height);
		y += height;
		add(ignore);
		
		// Show the action separator
		tmpsep = new JSeparator();
		tmpsep.setBounds(1, y, width, 5);
		y += 5;
		add(tmpsep);
		
		tmplbl = new JLabel("Actions");
		tmplbl.setFont(tmplbl.getFont().deriveFont(Font.BOLD, 14));
		tmplbl.setBounds(1, y, width, 15);
		y += 20;
		add(tmplbl);
		
		// Show the start-save-cancel
		tmpbut = new JButton("Cancel");
		tmpbut.addActionListener(this);
		tmpbut.setBounds(1, y, 80, 20);
		add(tmpbut);
		
		tmpbut = new JButton("Save");
		tmpbut.addActionListener(this);
		tmpbut.setBounds(width/2-40, y, 80, 20);
		add(tmpbut);
		
		tmpbut = new JButton("Start");
		tmpbut.addActionListener(this);
		tmpbut.setBounds(width-100, y, 80, 20);
		add(tmpbut);
		
		y += 20;
		

		parentViewer.AnimateChangeComponent(this, width, y+50);
	}

	public void actionPerformed(ActionEvent arg0) {
		String profile = "";
		
		// Get all the text from the sections
		profile += sSource.getConfig() + "\n";
		profile += sDestination.getConfig() + "\n";
		profile += sIgnore.getConfig();
		
		// Start button
		if (arg0.getActionCommand().compareTo("Start") == 0) {
			// Change the slide for the backup
			new Backup(profile, parentViewer);
		}
		
		// Cancel button
		if (arg0.getActionCommand().compareTo("Cancel") == 0) {
			parentViewer.ToStart();
		}
		
		// Save button
		if (arg0.getActionCommand().compareTo("Save") == 0) {
			String name = JOptionPane.showInputDialog("What is the profile name that you want?");
			if (name != null) {
				if (!Profile.SaveProfile(name, profile)) JOptionPane.showMessageDialog(this, "The profile could not be saved. Don't put special caracters in it.");
			}
		}
	}
}


